package com.donleo.word.controller;


import com.donleo.common.api.CommonResult;
import com.donleo.common.model.Word;
import com.donleo.common.relation.WordRecord;
import com.donleo.word.service.WordService;
import com.donleo.word.vo.WordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 单词表 前端控制器
 * </p>
 *
 * @author liangd
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/word")
public class WordController {

    @Autowired
    private WordService wordService;

    /**
     * 添加
     */
    @PostMapping("/add")
    public CommonResult add(@RequestParam("userCode") String userCode,
                            @RequestBody Word word) {
        return wordService.add(userCode, word);
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public CommonResult delete(Integer id){
        return wordService.delete(id);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public CommonResult update(@RequestParam("userCode") String userCode,
                               @RequestBody Word word ){
        Assert.notNull(word.getId(),"Id is to be null");
        return wordService.updateWord(userCode,word);
    }

    /**
     * 查询单个
     */
    @GetMapping("/findById")
    public CommonResult findById(Integer id){
        Word word=wordService.findById(id);
        return CommonResult.success(word);
    }

    /**
     * 模糊查询
     */
    @GetMapping("/findAll")
    public CommonResult findAll(@RequestParam String userCode, WordVo wordVo) {
        List<WordVo> list = wordService.findAll(userCode, wordVo);
        long total = wordService.getCount(wordVo);
        return CommonResult.success(total, list);
    }

    /**
     * 模糊查询用户发布或收藏的单词
     */
    @GetMapping("/findUserWord")
    public CommonResult findUserWord(@RequestParam String userCode,
                                     @RequestParam Integer type,
                                     WordVo wordVo){
        List<WordVo> list = wordService.findUserWord(userCode,type, wordVo);
        long total = wordService.getUserWordCount(userCode,type,wordVo);
        return CommonResult.success(total, list);
    }

    /**
     * 用户点赞
     */
    @PostMapping("/userPraise")
    public CommonResult userPraise(@RequestParam Integer praiseStatus,
                                   @RequestBody WordRecord wordRecord) {
        return wordService.userPraise(praiseStatus, wordRecord);
    }

    /**
     * 用户收藏
     */
    @PostMapping("/userCollections")
    public CommonResult userCollections(@RequestParam Integer collectionStatus,
                                        @RequestBody WordRecord wordRecord) {
        return wordService.userCollections(collectionStatus, wordRecord);
    }

    /**
     * 导出Excel
     */
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody List<Word> word, HttpServletResponse response){
        wordService.exportExcel(word,response);
    }

}
